home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / sos3-2.lha / src / demo / demo_main.c
C/C++ Source or Header  |  1992-01-23  |  8KB  |  346 lines

  1. /* --------------------------------------------------------------------------
  2.  * Copyright 1992 by Forschungszentrum Informatik (FZI)
  3.  *
  4.  * You can use and distribute this software under the terms of the licence
  5.  * you should have received along with this program.
  6.  * If not or if you want additional information, write to
  7.  * Forschungszentrum Informatik, "STONE", Haid-und-Neu-Strasse 10-14,
  8.  * D-7500 Karlsruhe 1, Germany.
  9.  * --------------------------------------------------------------------------
  10.  */
  11. // **************************************************************************
  12. // Module demo                   ??/??/89                        Joerg Wienke
  13. //
  14. // **************************************************************************
  15. // Simple non-graphic tool to inspect the meta-database (small test program)
  16. // **************************************************************************
  17.  
  18. #include <stream.h>
  19. #include <string.h>
  20. #include "sos.h"
  21. #include "smg.h"
  22. #include "mta_use.h"
  23. #include "dir_use.h"
  24.  
  25. // *******
  26. smg_String type_kind (sos_Type type)
  27. {
  28.   if (type.has_type (sos_Union_type_type))
  29.     return "Union    ";
  30.   if (type.has_type (sos_Typedef_type_type))
  31.      return "Typedef  ";
  32.   if (type.has_type (sos_Extern_type_type))
  33.      return "Extern   ";
  34.   if (type.has_type (sos_Class_type_type))
  35.      return "Class    ";
  36.   if (type.has_type (sos_Enum_type_type))
  37.      return "Enum     ";
  38.   return    "         ";
  39. }
  40.  
  41. // *******
  42. void get_meth (sos_Method_List ml)
  43. {
  44.    agg_iterate (ml, sos_Method m)
  45.    {  cout << "   ";
  46.       if (m.get_is_static() == TRUE)
  47.      cout << "static ";
  48.       cout << m.get_result_type ().make_type_name() << " " 
  49.        << m.get_name() << " ";
  50.       
  51.       sos_Param_List pl = m.get_params ();
  52.       cout << "(";
  53.       sos_Bool is_first = TRUE;
  54.       agg_iterate (pl, sos_Param p)
  55.       {  if (is_first)
  56.         is_first = FALSE;
  57.      else
  58.         cout << ", ";
  59.      cout << p.get_type_name().make_type_name(); 
  60.       }
  61.       agg_iterate_end (pl, p);
  62.       cout << ");\n";
  63.    }
  64.    agg_iterate_end (ml, m);
  65. }
  66.  
  67. // *******
  68. void type_menu (int& choice, sos_Schema_module& sm, sos_String& str)
  69. {  smg_String t_kind;
  70.    sos_Type type;
  71.    sos_Type_table type_table;
  72.  
  73.    cout << "\f";
  74.  
  75.    type_table = sm.get_type_table ();
  76.    type = type_table [str];
  77.  
  78.    if (type.has_type (sos_Class_type_type))
  79.    {
  80.       cout << "class " << str;
  81.  
  82.       sos_Class_type ct = sos_Class_type::make(type);
  83.  
  84.    // print generic parameters
  85.    // ========================
  86.  
  87.       sos_Gen_param_List gpl;
  88.       gpl = ct.get_gen_params ();
  89.  
  90.       if (gpl != NO_OBJECT)
  91.       {  cout << "<";
  92.      sos_Bool is_first = TRUE;
  93.      agg_iterate (gpl, sos_Gen_param gp)
  94.      {  if (is_first)
  95.            is_first = FALSE;
  96.         else
  97.            cout << ", ";
  98.  
  99.         cout << gp.get_name ();
  100.      }
  101.      agg_iterate_end (gpl, gp);
  102.      cout << "> ";
  103.       }
  104.  
  105.    // print parameters
  106.    // ================
  107.  
  108.       sos_Param_List pl;
  109.       pl = ct.get_create_params ();
  110.  
  111.       if (pl != NO_OBJECT)
  112.       {  cout << "(";
  113.      sos_Bool is_first = TRUE;
  114.      agg_iterate (pl, sos_Param p)
  115.      {  if (is_first)
  116.            is_first = FALSE;
  117.         else
  118.            cout << ", ";
  119.  
  120.         cout << p.get_type_name ().make_type_name();
  121.         sos_String n = p.get_name();
  122.         if (n != NO_OBJECT)
  123.            cout << " " << n;
  124.      }
  125.      agg_iterate_end (pl, p);
  126.      cout << ") ";
  127.       }
  128.  
  129.    // print super_classes
  130.    // ===================
  131.  
  132.       sos_Type_name_List tnl;
  133.       tnl = ct.get_super_classes ();
  134.     
  135.       sos_Bool is_first = TRUE;
  136.       agg_iterate (tnl, sos_Type_name tn)
  137.       {  if (is_first)
  138.      {  is_first=FALSE;
  139.         cout << ": ";
  140.      }
  141.      else
  142.         cout << ", ";
  143.  
  144.      cout << tn.make_type_name();
  145.       }
  146.       agg_iterate_end (tnl, tn);
  147.  
  148.    // ===========================
  149.  
  150.       cout << "\n{\n";
  151.  
  152.       get_meth (ct.get_methods());
  153.       get_meth (ct.get_comp_methods());
  154.       get_meth (ct.get_static_methods());
  155.  
  156.       cout << "}\n";
  157.  
  158.    }
  159.  
  160.    else if (type.has_type (sos_Union_type_type))
  161.    {
  162.       sos_Union_type ut = sos_Union_type::make(type);
  163.  
  164.       sos_Type_name_List lt;
  165.  
  166.       cout << "union " << str << "\n{\n";
  167.       lt = ut.get_uniteds ();
  168.       
  169.       agg_iterate (lt, sos_Type_name tn)
  170.       {  cout << "   " << tn.make_type_name() << ";\n";
  171.       }     
  172.       agg_iterate_end (lt, tn);
  173.       cout << "}\n";
  174.    }
  175.  
  176.    else if (type.has_type (sos_Typedef_type_type))
  177.    {
  178.       sos_Typedef_type tt = sos_Typedef_type::make(type);
  179.  
  180.       cout << "typedef "
  181.            << "<" << tt.get_type_name ().make_type_name() << ">"
  182.            << " " << str << "\n";
  183.    }
  184.  
  185.    else if (type.has_type (sos_Extern_type_type))
  186.    {
  187.       cout << "\n\n       no further information \n";
  188.    }
  189.  
  190.    else if (type.has_type (sos_Enum_type_type))
  191.    {
  192.       sos_Enum_type et = sos_Enum_type::make(type);
  193.      
  194.       sos_String_List sl;
  195.  
  196.       cout << "       enum " << str << " {";
  197.       sl = et.get_literals ();
  198.     
  199.       sos_Bool is_first = TRUE;
  200.       agg_iterate (sl, sos_String s)
  201.       {  if (is_first)
  202.         is_first = FALSE;
  203.      else
  204.         cout << ", ";
  205.      cout << s;
  206.       }
  207.       agg_iterate_end (sl, s);
  208.       cout << "};";
  209.    }
  210.  
  211.    cout << "\n\n       (0) RETURN\n";
  212.    cout << "\n\n       CHOICE: ";
  213.    cin >> choice;
  214.  
  215.    if (choice == 0)
  216.    {  sos_Imports imports = sm.get_imports();
  217.       agg_iterate (imports, sos_Schema_module imported)
  218.       {  imported.container().close ();
  219.       }
  220.       agg_iterate_end (imports, imported);
  221.       sm.container ().close ();
  222.       return;
  223.    }
  224. }
  225.  
  226. // *******
  227. void schema_menu (int& choice, sos_Schema_module& sm, sos_String& str)
  228. {  sos_Type_table tt;
  229.    sos_String a[100];
  230.  
  231.    cout << "\f";
  232.  
  233.    sm.open_imports ();
  234.    tt = sm.get_type_table ();
  235.  
  236.    int i = 1;
  237.    agg_iterate_association (tt, sos_String name, sos_Type tp)
  238.    {  char is [10];
  239.       a[i] = name;
  240.       sprintf (is, "(%d) ", i);
  241. #ifdef ATT
  242.       char _is [12];
  243.       sprintf (_is, "%11s", is);
  244.       cout << _is
  245. #else
  246.       cout << form("%11s",is)
  247. #endif
  248.        << type_kind (tp) << name << "\n";
  249.       i++;
  250.    }
  251.    agg_iterate_association_end (tt, name, tp);
  252.  
  253.    cout << "\n       (0) RETURN\n";
  254.    cout << "\n       CHOICE: ";
  255.    cin >> choice;
  256.  
  257.    if (choice == 0)
  258.    {  sm.close_imports ();
  259.       sm.container().close ();
  260.    }
  261.    else
  262.       str = a[choice];
  263. }
  264.  
  265. // *******
  266. void schema_dir_menu (int& choice, 
  267.               sos_Schema_module_Directory& sd, sos_Schema_module& sm)
  268. {  sos_Container sm_container;
  269.    sos_String a[10];
  270.    sos_Imports imports;
  271.  
  272.    cout << "\f";    // clear screen
  273.  
  274.    int i = 1;
  275.    agg_iterate_association (sd, sos_String name, sos_Schema_module mdl)
  276.    {  a[i] = name;
  277.       sos_Cstring sname = name.make_Cstring ();
  278. #ifdef GNU
  279.       cout << "\n          (" << i << ") " << form("%6s",sname);
  280. #else
  281.       char _sname[7];
  282.       sprintf (_sname, "%6s", sname);
  283.       cout << "\n          (" << i << ") " << _sname;
  284. #endif
  285.       delete sname;
  286.  
  287.    // imports
  288.       
  289.       sm_container = mdl.container ();
  290.       sm_container.open ( READING, TESTING );
  291.  
  292.       imports = mdl.get_imports();
  293.  
  294.       cout << "   imports: ";
  295.       agg_iterate (imports, sos_Schema_module imported)
  296.       {  cout << imported.get_name () << "  ";
  297.       }
  298.       agg_iterate_end (imports, imported);
  299.       cout << "\n";
  300.       i++;
  301.  
  302.       agg_iterate (imports, sos_Schema_module imported)
  303.       {  imported.container().close ();
  304.       }
  305.       agg_iterate_end (imports, imported);
  306.  
  307.       sm_container.close ();
  308.    }
  309.    agg_iterate_association_end (sd, name, mdl);
  310.  
  311.    cout << "\n\n          (0) QUIT\n";
  312.    cout << "\n\n          CHOICE: ";
  313.    cin >> choice;
  314.  
  315.    if (choice != 0)
  316.       sm = sd [ a[choice] ];
  317. }
  318.  
  319. // *******
  320. main (int argc, char *argv[])
  321. {  
  322.    int choice1, choice2, choice3;
  323.    sos_String str;
  324.    sos_Schema_module sm;
  325.    sos_Schema_module_Directory sd;
  326.  
  327.    sos_init (argc, argv);
  328.  
  329.    sd = sos_Schema_module::schema_dir();
  330.    sd.container().open (READING, WAITING);
  331.  
  332.    for (choice1 = 1;  choice1; )
  333.    {  schema_dir_menu (choice1, sd, sm);
  334.  
  335.       for (choice2 = 1;  choice2 && choice1; )
  336.       {  schema_menu (choice2, sm, str);
  337.  
  338.      for (choice3 = 1;  choice3 && choice2; )
  339.         type_menu (choice3, sm, str);
  340.       }
  341.    }
  342.  
  343.    sd.container().close ();
  344.    exit (0);
  345. }
  346.